package com.bosto.jiramanager;

import java.util.ArrayList;
import java.util.List;

import com.bosto.jiramanager.entity.*;
import com.bosto.jiramanager.helper.SharedPreferencesHelper;
import com.bosto.jiramanager.helper.SqlService;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class PopAddJiraWinActivity extends Activity implements OnClickListener {
	private EditText et;
	private Spinner spinnerType, spinnerStatus;
	private Button btn1,btn2;
	private ArrayAdapter<String> adapterType, adapterStatus;
	private SqlService sqlHelper;
	private SharedPreferencesHelper sph;
	private Context context;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.popwin);
		context=((ApplicationClass)getApplication()).getContext();
		InitView();
		sph=new SharedPreferencesHelper(context,
				Contants.DBNAMESET);
		String dbName=sph.GetValue(Contants.DBCHOSEN);
		sqlHelper=new SqlService(context,dbName+Contants.DBNAMECOMMENT,1);
	}

	void InitView() {
		et = (EditText) findViewById(R.id.jira_no_edittext);
		spinnerType = (Spinner) findViewById(R.id.spinner_type);
		spinnerStatus = (Spinner) findViewById(R.id.spinner_status);
		btn1=(Button) findViewById(R.id.btn_pop_add);
		btn2=(Button) findViewById(R.id.btn_pop_quit);
		btn1.setOnClickListener(this);
		btn2.setOnClickListener(this);
		List<String> listType = new ArrayList<String>();
		listType.add(Contants.TYPE_BUG);
		listType.add(Contants.TYPE_CODE);
		listType.add(Contants.TYPE_REQ);


		adapterType = new ArrayAdapter<String>(this,
				R.layout.custom_spinner_item, listType);
		// Specify the layout to use when the list of choices appears
		adapterType
				.setDropDownViewResource(R.layout.custom_spinner_dropdown_item);
		// Apply the adapter to the spinner
		spinnerType.setAdapter(adapterType);
		
		List<String> listStatus = new ArrayList<String>();
		listStatus.add(Contants.STATUS_OPEN);
		listStatus.add(Contants.STATUS_IMPLEMENTED);
		listStatus.add(Contants.STATUS_REVIEWED);
		listStatus.add(Contants.STATUS_MERGERD);
		listStatus.add(Contants.STATUS_VERIFIED);
		
		adapterStatus = new ArrayAdapter<String>(this,
				R.layout.custom_spinner_item, listStatus);
		// Specify the layout to use when the list of choices appears
		adapterStatus
				.setDropDownViewResource(R.layout.custom_spinner_dropdown_item);
		// Apply the adapter to the spinner
		spinnerStatus.setAdapter(adapterStatus);
		
		

	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v.getId()==R.id.btn_pop_add){
			String jira_no=Contants.JIRAFIRST+et.getText().toString();
			String jira_type= spinnerType.getItemAtPosition(spinnerType.getSelectedItemPosition()).toString();
			String jira_status=spinnerStatus.getItemAtPosition(spinnerStatus.getSelectedItemPosition()).toString();
			long rows=sqlHelper.insertData(jira_no, jira_status, jira_type);
			Toast.makeText(PopAddJiraWinActivity.this, rows+Contants.ADDDBMESSAGE,
					Toast.LENGTH_LONG).show();
		}else if(v.getId()==R.id.btn_pop_quit){
			onBackPressed();
		}
	}
	
	
	
	
	
	

}
